import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/base/rating_star_widget.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';

import '../../../bean/bean_train.dart';
import '../../../net/dio_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///课程简介
class CourseIntroducePage extends StatefulWidget {
  final BeanVideoCourse beanVideoCourse;
  final int trainId;

  const CourseIntroducePage({
    Key key,
    this.beanVideoCourse,
    this.trainId,
  }) : super(key: key);

  @override
  _CourseIntroducePageState createState() => _CourseIntroducePageState();
}

class _CourseIntroducePageState extends State<CourseIntroducePage> {
  CancelToken _cancelToken;
  TrainBean _trainBean;

  @override
  void initState() {
    super.initState();
    if (widget.trainId != null) {
      Future.delayed(Duration.zero, () async {
        LogUtils.e("查询培训详情--");
        _cancelToken = new CancelToken();
        Map<String, dynamic> monthMap = {"trainId": widget.trainId};
        //获取培训的信息
        ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
          url: HttpHelper.trainDetailsInfoBase,
          queryParameters: monthMap,
          cancelTag: _cancelToken,
        );
        if (statisRresponseInfo.success) {
          if (statisRresponseInfo.data != null) {
            LogUtils.e("查询培训详情-- 1");
            _trainBean = TrainBean.fromJson(statisRresponseInfo.data);
          } else {
            LogUtils.e("查询培训详情-- 2");
          }
        } else {
          LogUtils.e("查询培训详情-- 3");
          _trainBean = null;
        }
        setState(() {});
      });
    }
  }

  @override
  void dispose() {
    if (_cancelToken != null && !_cancelToken.isCancelled) {
      //取消下载
      _cancelToken.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[200],
      child: SingleChildScrollView(
        child: Column(
          children: [
            buildCourseTitle(),
            buildTrainDetains(),
            buildCourseIntroduce(),
          ],
        ),
      ),
    );
  }

  buildCourseTitle() {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.all(12),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "${widget.beanVideoCourse.videoCourseName}",
            style: TextStyle(fontWeight: FontWeight.w600, fontSize: 17),
          ),
          SizedBox(
            height: 10,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                width: 110,
                child: RatingStarWidget(
                  starSize: 16,
                  //评分回调的数据类型
                  starType: StarType.iStar,
                  //选中的五角星的高亮颜色
                  selectColor: Colors.deepOrange[400],
                  //背景显示的五角星的颜色
                  normalColor: Colors.grey[400],
                  //评分五角星的间隔
                  space: 1,
                  //是否可滑动或者是点击修改评分值
                  isAnimation: false,
                  //五角星个数
                  totalNumber: 6,
                  //高亮显示的五角星的个数
                  initNumber: 4,
                  //初始化选择五角星的加载动画  默认无动画
                  starAnimationType: StarAnimationType.fade,
                  //五角星的加载动画的时间 默认 800毫秒
                  animationMills: 1000,
                  //滑动或者点击评分修改后的回调
                  selectCallback: (value) {
                    print("选择的评分  $value");
                  },
                ),
              ),
              Text(
                "评分 5.0",
                softWrap: true,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.right,
                maxLines: 3,
                style: TextStyle(fontSize: 12, color: Colors.black38),
              ),
              SizedBox(
                width: 16,
              ),
              Text(
                "${widget.beanVideoCourse.studyUserNumber}人在学",
                softWrap: true,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.right,
                maxLines: 3,
                style: TextStyle(fontSize: 12, color: Colors.black38),
              )
            ],
          ),
        ],
      ),
    );
  }

  buildTrainDetains() {
    if (_trainBean == null) {
      return SizedBox();
    }
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.all(12),
      margin: EdgeInsets.only(top: 12),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                "培训名称:",
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                ),
              ),
              SizedBox(width: 10),
              Expanded(
                child: Text(
                  "${_trainBean.trainName}",
                  style: TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: Colors.grey[800],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 3),
          (_trainBean.trainRemark == null || _trainBean.trainRemark.isEmpty)
              ? SizedBox()
              : Row(
                  children: [
                    Text(
                      "培训说明:",
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: Colors.black,
                      ),
                    ),
                    Text(
                      "${_trainBean.trainRemark ?? ""}",
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.w400,
                        color: Colors.grey[800],
                      ),
                    ),
                  ],
                ),
          SizedBox(height: 3),
          Row(
            children: [
              Text(
                "开始时间:",
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                ),
              ),
              SizedBox(width: 10),
              Expanded(
                child: Text(
                  "${_trainBean.startStudyTime}",
                  style: TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: Colors.grey[800],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 3),
          Row(
            children: [
              Text(
                "结束时间:",
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                ),
              ),
              SizedBox(width: 10),
              Expanded(
                child: Text(
                  "${_trainBean.endStudyTime}",
                  style: TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: Colors.grey[800],
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  buildCourseIntroduce() {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.all(12),
      margin: EdgeInsets.only(top: 12),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "课程介绍",
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Colors.black,
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Text(
            "${widget.beanVideoCourse.videoCourseInfo}",
            style: TextStyle(
              fontSize: 13,
              fontWeight: FontWeight.w400,
              color: Colors.grey[800],
            ),
          ),
        ],
      ),
    );
  }
}
